package com.team.common.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;

import java.util.List;
@Slf4j
public class MybatisPlugsGeneratorUtil {

    public static void start(String moduleName,String parentPackageName,String[] tables) {
        AutoGenerator autoGenerator = new AutoGenerator();

        autoGenerator.setGlobalConfig(globalConfig());
        autoGenerator.setDataSource(dataSourceConfig());
        autoGenerator.setPackageInfo(packageConfig(moduleName,parentPackageName));
        autoGenerator.setCfg(injectionConfig(moduleName));
        autoGenerator.setTemplate(templateConfig());
        autoGenerator.setTemplateEngine(new VelocityTemplateEngine());
        autoGenerator.setStrategy(strategyConfig(moduleName,tables));
        autoGenerator.execute();
    }


    private static GlobalConfig globalConfig(){
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
        globalConfig.setAuthor("team");
        globalConfig.setOpen(false);
        return globalConfig;
    }

    private static DataSourceConfig dataSourceConfig(){
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://120.92.140.48:3306/teamx?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC&createDatabaseIfNotExist=true");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");
        return dataSourceConfig;
    }


    private static PackageConfig packageConfig(String moduleName,String parentPackageName){
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName(moduleName);
        packageConfig.setParent(parentPackageName);
        return packageConfig;
    }


    private static InjectionConfig injectionConfig(String moduleName){
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                log.info("init injection config.");
            }
        };
        String templatePath = "/templates/mapper.xml.vm";
        List<FileOutConfig> fileOutConfigList = Lists.newArrayList();
        fileOutConfigList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String mapperPath =
                        System.getProperty("user.dir")+"/src/main/resources/mapper/";
                return mapperPath+moduleName+"/"+tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
            }
        });
        injectionConfig.setFileOutConfigList(fileOutConfigList);
        return injectionConfig;
    }

    private static TemplateConfig templateConfig(){
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        return templateConfig;
    }


    private static StrategyConfig strategyConfig(String moduleName,String... tables){
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setEntityLombokModel(true);
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setInclude(tables);
        strategyConfig.setControllerMappingHyphenStyle(true);
        strategyConfig.setTablePrefix(moduleName+"_");
        return strategyConfig;
    }
}
